
import structPageOptimize from '../../scripts/base/structPageOptimize'
import scrollOpt from '../base/scrollOptimize'
import { WX_AD_CONFIG, Sdk_Platform, VIVO_AD_CONFIG } from '../../frame/constants';
import { UI_Layer, UI_Listen } from '../../config/ui_def'
cc.Class({
    extends: structPageOptimize,

    properties: {
        adNode: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        // let passTable = he.db.getTable('Pass')
        // this.pageGrp = []
        // if (passTable && passTable.length > 0) {
        //     for (let i = 0; i < passTable.length; i++) {
        //         if (this.pageGrp.length == 0) {
        //             this.pageGrp.push(passTable[i].main_pass)
        //         } else {
        //             let isHave = false
        //             for (let j = 0; j < this.pageGrp.length; j++) {
        //                 if (this.pageGrp[j] == passTable[i].main_pass) {
        //                     isHave = true
        //                     break
        //                 }
        //             }
        //             if (!isHave) {
        //                 this.pageGrp.push(passTable[i].main_pass)
        //             }
        //         }
        //     }
        // }
    },

    // update (dt) {},

    initPage() {
        if (!he.pool.getPool('passItemBig')) {
            he.pool.createPoolSync('passItemBig', 5, cc.Prefab, 'uimod/passItemBig')
        }
        if (!he.pool.getPool('passItem')) {
            he.pool.createPoolSync('passItem', 10, cc.Prefab, 'uimod/passItem')
        }
    },

    updatePage(param) {
        // this.scrollInit()
        this.cleartweenGrp()
        let passTable = he.db.getTable('Pass')
        this.pageGrp = []
        if (passTable && passTable.length > 0) {
            for (let i = 0; i < passTable.length; i++) {
                if (this.pageGrp.length == 0) {
                    this.pageGrp.push(passTable[i].main_pass)
                } else {
                    let isHave = false
                    for (let j = 0; j < this.pageGrp.length; j++) {
                        if (this.pageGrp[j] == passTable[i].main_pass) {
                            isHave = true
                            break
                        }
                    }
                    if (!isHave) {
                        this.pageGrp.push(passTable[i].main_pass)
                    }
                }
            }
        }
        this.pageIndex = 0
        if (param.pageIndex >= 0) {
            this.pageIndex = param.pageIndex
        }
        this.passLen = he.local.getJson('pass_list', 'user').length
        gg.eventManager.emit('main', { type: UI_Listen.MAIN_NEWPAGE, pageName: 'rankMainPage' })
        this.adSwitch(false)
        this.structPageInit(this.pageGrp, this.backInfoInit, this.passListUpdate, 0, this.pageInfoUpdate)
    },

    closeUI() {
        this.closeTouchListen()
        this.adSwitch(false)
    },

    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.VIVO:
                    // this.adNode.name = VIVO_AD_CONFIG.bannerId[0]
                    // he.ad.changeBanner(status, this.adNode)
                    // he.ad.interstitialShow()
                    break
            }
        } else {
            switch (common.sdk_env) {
                case Sdk_Platform.VIVO:
                    // this.adNode.name = VIVO_AD_CONFIG.bannerId[0]
                    // he.ad.changeBanner(status, this.adNode)
                    break
            }
        }

    },

    closeServer() {
        gg.uiManager.closePage(UI_Layer.Cover, 'passListPage')
        this.uiBase.closePage()
        gg.eventManager.emit('main', { type: UI_Listen.MAIN_TOMAIN })
    },
    pageInfoUpdate(index, info) {
        // this.cardsArray = this.content.children;
        let labInfo
        if (info >= 0) {
            labInfo = info
        } else {
            labInfo = null
        }
        let lab = this.cardsArray[index].getChildByName('New Label').getComponent(cc.Label)
        if (labInfo == null) {
            lab.string = ''
            this.cardsArray[index].active = false
        } else {
            this.cardsArray[index].active = true
            // console.log(this.pageIndex)
            if (info <= (this.passLen / 10 + 1)) {
                this.cardsArray[index].getChildByName('missionlock').active = false
            } else {
                this.cardsArray[index].getChildByName('missionlock').active = true
            }

            this.cardsArray[index].getComponent(cc.Sprite).spriteFrame = he.load.get('newPageList', cc.SpriteAtlas, 'plist').getSpriteFrame(`${labInfo}`)
            lab.string = `第${labInfo}关`
        }
    },

    backInfoInit(right) {
        let inputIndex = this.pageIndex
        if (right) {
            // console.log(this.passLen / 10 + 1)
            // console.log(this.pageGrp[inputIndex + 1])
            // if ((this.passLen / 10 + 1) < this.pageGrp[inputIndex + 1]) {
            //     return
            // }
            if (inputIndex + 3 <= this.pageGrp.length - 1) {
                this.pageInfoUpdate(0, this.pageGrp[inputIndex + 3])
            } else {
                this.pageInfoUpdate(0, null)
            }
        } else {

            if (inputIndex - 3 >= 0) {
                this.pageInfoUpdate(0, this.pageGrp[inputIndex - 3])
            } else {
                this.pageInfoUpdate(0, null)
            }
        }
    },

    passListUpdate() {
        let pass_main = this.pageGrp[this.pageIndex]

        gg.uiManager.addPage(UI_Layer.Cover, {
            name: 'passListPage',
            pass_mainId: pass_main
        })
    },
});
